<?php
namespace App\Controllers;

use App\Services\PartnerService;
use App\Services\SearchTimeLimitService;
use App\Services\StatisticsService;
use common\enum\CommonEnum;
use common\utils\SysCode;
use Illuminate\Http\Request;

/**
 * 报表统计Controller
 * Class StateController
 * @package App\Controllers
 */
class StateController
{
    // 总报表
    public static function getAllStatistics(Request $request)
    {
        $params = $request->input();
        $today = date ('Y-m-d');
        $params['date_begin'] = isset($params['date_begin']) ? $params['date_begin'] : $today;
        $params['date_end'] = isset($params['date_end']) ? $params['date_end'] : $today;
        $params['page'] = isset($params['page']) ? $params['page'] : CommonEnum::PAGE_DEFAULT;
        $params['page_size'] = isset($params['page_size']) ? $params['page_size'] : CommonEnum::PAGE_SIZE_TWENTY;
        // 排序设置
        $params['order'] = (isset($params['order']) && in_array($params['order'], ['profit','bet_amount','validbet','bet_number','bonus_amount','contribute_amount'])) ? $params['order'] : 'profit';
        $params['desc'] = (isset($params['desc']) && in_array($params['desc'], ['asc','desc','ASC','DESC'])) ? $params['desc'] : 'desc';
        if($params['date_begin']>$today || $params['date_end']>$today){
            error_exit(SysCode::INVALID_ARGUMENT, '时间不能是未来时间');
        }
        if($params['date_begin']>$params['date_end']){
            error_exit(SysCode::INVALID_ARGUMENT, '开始时间不可超过截止时间');
        }
        //搜索时间限制
        if (!empty($params['date_begin']) && !empty($params['date_end'])) {
            SearchTimeLimitService::timeLimit($params['date_begin'],$params['date_end'],SearchTimeLimitService::SIX_MONTH);
        }
        $data = StatisticsService::getAllStatistics($params);
        amount_format_array($data, ['bet_amount', 'validbet','contribute_amount','bet_total_amount','profit']);
        return $data;
    }

    // 游戏类型获取
    public static function getAllGame(Request $request)
    {
        $params = $request->input();
        $type = 0;
        //type=0表示获取全部
        if(isset($params['type']) && !in_array($params['type'],[0, CommonEnum::GAME_EGAME, CommonEnum::GAME_LIVE,CommonEnum::GAME_SPORT,CommonEnum::GAME_ESPORT,CommonEnum::GAME_LOTTERY,CommonEnum::GAME_CARD])){
            error_exit(SysCode::INVALID_ARGUMENT);
        }
        if(isset($params['type']) && in_array($params['type'],[CommonEnum::GAME_EGAME, CommonEnum::GAME_LIVE,CommonEnum::GAME_SPORT,CommonEnum::GAME_ESPORT,CommonEnum::GAME_LOTTERY,CommonEnum::GAME_CARD])) {
            $type = $params['type'];
        }
        return PartnerService::getPartnerConfigList($type);
    }

    // 游戏报表
    public static function getGameStatistics(Request $request)
    {
        $params = $request->input();
        $today = date ('Y-m-d');
        $params['type'] = isset($params['type']) ? $params['type'] :  1;//1 系统游戏报表  2 第三方对账游戏报表 默认系统游戏报表
        $params['date_begin'] = isset($params['date_begin']) ? $params['date_begin'] :  $today;
        $params['date_end'] = isset($params['date_end']) ? $params['date_end'] : $today;
        $params['page'] = isset($params['page']) ? $params['page'] : CommonEnum::PAGE_DEFAULT;
        $params['page_size'] = isset($params['page_size']) ? $params['page_size'] : CommonEnum::PAGE_SIZE_TWENTY;
        // 排序设置
        $params['order'] = (isset($params['order']) && in_array($params['order'], ['profit','bet_amount','validbet','bet_number','bonus_amount','contribute_amount'])) ? $params['order'] : 'profit';
        $params['desc'] = (isset($params['desc']) && in_array($params['desc'], ['asc','desc','ASC','DESC'])) ? $params['desc'] : 'desc';
        //搜索时间限制
        if (!empty($params['date_begin']) && !empty($params['date_end'])) {
            SearchTimeLimitService::timeLimit($params['date_begin'],$params['date_end'],SearchTimeLimitService::SIX_MONTH);
        }
        if($params['date_begin']>$today || $params['date_end']>$today){
            error_exit(SysCode::INVALID_ARGUMENT, '时间不能是未来时间');
        }
        if($params['date_begin']>$params['date_end']){
            error_exit(SysCode::INVALID_ARGUMENT, '开始时间不可超过截止时间');
        }
        $data = StatisticsService::getGameStatistics($params);
        amount_format_array($data, ['bet_amount', 'validbet','contribute_amount','bet_total_amount','profit']);
        return $data;
    }

    public static function exportAllStatistics(Request $request)
    {
        $params = $request->input();
        $today = date ('Y-m-d');
        $params['date_begin'] = isset($params['date_begin']) ? $params['date_begin'] : $today;
        $params['date_end'] = isset($params['date_end']) ? $params['date_end'] : $today;
        $params['page'] = isset($params['page']) ? $params['page'] : CommonEnum::PAGE_DEFAULT;
        $params['page_size'] = isset($params['page_size']) ? $params['page_size'] : CommonEnum::PAGE_SIZE_TWENTY;
        // 排序设置
        $params['order'] = (isset($params['order']) && in_array($params['order'], ['profit','bet_amount','validbet','bet_number','bonus_amount','contribute_amount'])) ? $params['order'] : 'profit';
        $params['desc'] = (isset($params['desc']) && in_array($params['desc'], ['asc','desc','ASC','DESC'])) ? $params['desc'] : 'desc';
        if($params['date_begin']>$today || $params['date_end']>$today){
            error_exit(SysCode::INVALID_ARGUMENT, '时间不能是未来时间');
        }
        if($params['date_begin']>$params['date_end']){
            error_exit(SysCode::INVALID_ARGUMENT, '开始时间不可超过截止时间');
        }
        return  StatisticsService::exportAllStatistics($params);
    }

}